/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.DeliverBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DeliverBillDao;
import com.minierp.dao.DeliverBillItemDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.PayModeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncDeliverBillFormCtrHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.CodeRule;
import com.minierp.model.DeliverBill;
import com.minierp.model.Department;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.Establishment;
import com.minierp.model.TallySet;
import com.minierp.service.DeliverBillService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class DeliverBillFormCtr extends SimpleFormController {
	private DeliverBillItemDao deliverBillItemDao;
	private DeliverBillDao deliverBillDao;
	private BusinessBillItemDao businessBillItemDao;
	private MakeTypeDao makeTypeDao;
	private PayModeDao payModeDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;
	private DeliverBillService deliverBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		refData.put("payModeList", this.payModeDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("DELIVER_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for deliver bill");
		} else {
			DeliverBillBean form;
			BusinessBill businessBill;
			List makeTypeList;
			if (id != null && !id.equals("")) {
				DeliverBill businessBillId1 = (DeliverBill) this.deliverBillDao
						.get(DeliverBill.class, Long.parseLong(id));
				if (businessBillId1 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				businessBill = (BusinessBill) this.deliverBillDao
						.get(BusinessBill.class,
								businessBillId1.getBusinessBillId());
				if (businessBill == null) {
					throw new Exception("Business bill not found for id:"
							+ businessBillId1.getBusinessBillId());
				}

				TallySet est1 = (TallySet) this.deliverBillDao.get(
						TallySet.class, businessBillId1.getTallySetId());
				Establishment dep1 = (Establishment) this.deliverBillDao.get(
						Establishment.class, businessBill.getClientId());
				Department bbItemList2 = (Department) this.deliverBillDao.get(
						Department.class, businessBill.getDepartmentId());
				makeTypeList = this.deliverBillItemDao
						.getListByDeliverBill(businessBillId1.getId());
				List bbItemList1 = this.businessBillItemDao
						.getList(businessBill.getId());
				List makeTypeList1 = this.makeTypeDao.getList(false);
				form = EncDeliverBillFormCtrHelper.doFormBackingObjectEdit(cr,
						businessBillId1, makeTypeList, dep1, bbItemList2,
						businessBill.getBillCode(),
						businessBill.getManagerName(),
						businessBill.getFavCost(), bbItemList1, makeTypeList1,
						est1);
			} else {
				String businessBillId = request
						.getParameter("business_bill_id");
				businessBill = (BusinessBill) this.deliverBillDao.get(
						BusinessBill.class, Long.parseLong(businessBillId));
				if (businessBill == null) {
					throw new Exception("Business bill not found for id:"
							+ businessBillId);
				}

				Establishment est = (Establishment) this.deliverBillDao.get(
						Establishment.class, businessBill.getClientId());
				if (est == null) {
					throw new Exception("Can\'t find client for id:"
							+ businessBill.getClientId());
				}

				Department dep = (Department) this.deliverBillDao.get(
						Department.class, businessBill.getDepartmentId());
				List bbItemList = this.businessBillItemDao
						.getListWidthExt(businessBill.getId());
				makeTypeList = this.makeTypeDao.getList(false);
				form = EncDeliverBillFormCtrHelper.doFormBackingObjectNew(
						request, cr, est, dep, businessBill, bbItemList,
						makeTypeList);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		DeliverBillBean form = (DeliverBillBean) command;
		EncDeliverBillFormCtrHelper.doOnBind(form, request, this.dataSource);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		DeliverBillBean form = (DeliverBillBean) command;
		DeliverBill obj = form.getDeliverBill();
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "DELI_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			obj.setExaminerId(ub.getStaffer().getId());
			obj.setExaminerName(ub.getStaffer().getStafferName());
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "DELI_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.deliverBillService.txUpdate(form, ent.getDeliAutoComp(),
						ub, this.deliverBillDao, this.deliverBillItemDao,
						this.businessBillItemDao, this.idGenerator);
			} catch (CannotAcquireLockException arg12) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg13) {
				EncCommonHelper.setAlertMessage(request, arg13.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		} else {
			try {
				this.deliverBillService.txInsert(form, ent.getDeliAutoComp(),
						ub, this.deliverBillDao, this.deliverBillItemDao,
						this.businessBillItemDao, this.serialNumberDao,
						this.idGenerator);
			} catch (CannotAcquireLockException arg14) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg15) {
				EncCommonHelper.setAlertMessage(request, arg15.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		return mv;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public DeliverBillDao getDeliverBillDao() {
		return this.deliverBillDao;
	}

	public void setDeliverBillDao(DeliverBillDao deliverBillDao) {
		this.deliverBillDao = deliverBillDao;
	}

	public DeliverBillItemDao getDeliverBillItemDao() {
		return this.deliverBillItemDao;
	}

	public void setDeliverBillItemDao(DeliverBillItemDao deliverBillItemDao) {
		this.deliverBillItemDao = deliverBillItemDao;
	}

	public BusinessBillItemDao getBusinessBillItemDao() {
		return this.businessBillItemDao;
	}

	public void setBusinessBillItemDao(BusinessBillItemDao businessBillItemDao) {
		this.businessBillItemDao = businessBillItemDao;
	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public DeliverBillService getDeliverBillService() {
		return this.deliverBillService;
	}

	public void setDeliverBillService(DeliverBillService deliverBillService) {
		this.deliverBillService = deliverBillService;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public PayModeDao getPayModeDao() {
		return this.payModeDao;
	}

	public void setPayModeDao(PayModeDao payModeDao) {
		this.payModeDao = payModeDao;
	}
}